package hong.jun.task;


import java.util.concurrent.*;

/**
 * 说明:任务线程池管理器
 * Created on 2023/10/19 14:56
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class TaskThreadPoolManger {
    /**
     * 用枚举来实现单例
     */
    private enum Singleton {
        INSTANCE;
        final TaskThreadPoolManger processor;

        Singleton() {
            this.processor = new TaskThreadPoolManger();
        }

        TaskThreadPoolManger getProcessor() {
            return processor;
        }
    }

    /**
     * 获取实例对象
     *
     */
    public static TaskThreadPoolManger getInstance() {
        return TaskThreadPoolManger.Singleton.INSTANCE.getProcessor();
    }

    //单线程池
    private final ExecutorService singleExecutorService;

    //多线程任务队列
    private final ExecutorService fixedexecutorService;

    //虚拟线程任务队列
    private final ExecutorService virtualExecutor;

    public TaskThreadPoolManger() {
        singleExecutorService = Executors.newSingleThreadExecutor();
        fixedexecutorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2,//核心线程数
                200,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>());
        virtualExecutor = Executors.newVirtualThreadPerTaskExecutor();
    }

    public ExecutorService getSingleThreadPool() {
        return singleExecutorService;
    }
    public ExecutorService getFixedThreadPool() {
        return fixedexecutorService;
    }

    /**
     * @Description:单线程任务队列增加
     * @Param: [task]
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/19
     */

    public void addSingleTask(ThreadTask task) {
        singleExecutorService.execute(task);
    }

    /**
     * @Description:多线程任务队列增加
     * @Param: [task]
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/19
     */

    public void addFixedTask(ThreadTask task) {
        fixedexecutorService.execute(task);
    }

    /**
     * 虚拟线程任务队列增加
     *
     * @Description:
     * @Param: [task]
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/19
     */
    public void addVirtualTask(ThreadTask task) {
        virtualExecutor.submit(task);
    }
}
